var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentArray:[],
    subArray:[
      {
        id:1,
        value:'语文'
      },
      {
        id:2,
        value:'数学'
      },
      {
        id:3,
        value:'英语'
      }
    ],
    subDisplay:'none',
    select:false,
    subjectDisplay:false,
    zheHeight: '',
    move: true,
    shoWhat: true,
    tanchuDis: false,
    id:'',
    title: '作业辅导',
    name: '李先生',
    sex: '男',
    address: '成都市郫都区红光镇西华大学999号',
    grade: '五年级',
    subject: '数学',
    teacherSex: '男',
    yaoqiu: '有家教经验，有责任心',
    salary: '50',
    pageNo:2,
    teachTime: '每周2次课;每次2小时',
    qingkuang: '孩子数学成绩不好',
    type:'',
    telephone:'',
    parentId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.getSystemInfo({
      success: function(res) {
        console.log(res)
        that.setData({
          zheHeight:res.windowHeight
        })
      },
    })
  },
  getContent: function (event) {
    var that = this
    this.setData({
      tanchuDis: !this.data.tanchuDis
    })
    console.log(event)
    var id = event.currentTarget.dataset.id
    console.log(id)
    wx.request({
      url: app.globalData.baseUrl + 'demand/demand/' + id,
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        var name;
        if (res.data.data.user.nickName == '') {
          name = '该用户未填写姓名'
        } else {
          name = res.data.data.user.nickName
        }
        var add
        if (res.data.data.position == '') {
          add = '该用户未填写地址'
        } else {
          add = res.data.data.position
        }  

        that.setData({
          title: res.data.data.title,
          name: name,
          address: add,
          type:res.data.data.type,
          grade:res.data.data.gradeId+'年级',
          id:res.data.data.userId,
          parentId: res.data.data.userId
        })
        if(that.data.type==3){
          that.setData({
            telephone:res.data.data.userTelephone
          })
        }
      }
    })
  },
  contactParent:function(event){
    var that = this
    var id = event.currentTarget.dataset.id
    var show = this.data.type
    console.log(show)
    var vip = wx.getStorageSync('vip')
    if (show==3) {
      if (vip) {
        console.log(that.data.telePhone)
        wx.showModal({
          title: '',
          content: '您每天有两次免费联系对方的机会',
          success: function (res) {
            wx.makePhoneCall({
              phoneNumber: that.data.telephone,
              success: function (res) {

                console.log(res)
                console.log('拨打电话成功')
              },
              fail: function (err) {
                console.log(that.data.telePhone)
                console.log(err)

              }
            })
          }
        })

      } else {
        wx.showModal({
          title: '',
          content: '您不是会员无法联系对方,是否成为会员?',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/geren/becomeVIP/becomeVIP',
              })
            }
          }
        })
      }

    } else {
      console.log(event)
      console.log("contact")
      wx.navigateTo({
        url: '/xiaoxi/chat/chat?id=' + id,
      })
    }

  },
  changeTanchu: function (event) {
    this.setData({
      tanchuDis: false
    })
  },
  toTeacherContent: function (event) {
    console.log(event)
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../teacher/teacher?id=' + id,
    })
  },


  selectSubject:function(event){
   wx.pageScrollTo({
     scrollTop: 0,
   })
   this.setData({
     subjectDisplay:!this.data.subjectDisplay,
     select:!this.data.select

   }) 
  },
  changeSubject:function(event){
    console.log(event)
    var id = event.currentTarget.dataset.id
    var that = this

    wx.request({
      url: app.globalData.baseUrl + 'demand/listall',
      data: {
        type: 1,
        courseId: id
      },
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        that.setData({
          studentArray:[]
        })
        try {
          var resData = res.data.data.data
          for (var i = 0; i < resData.length; i++) {
            var username = 'studentArray[' + i + '].username'
            var special = 'studentArray[' + i + '].special'
            var courseName = 'studentArray[' + i + '].courseName'
            var position = 'studentArray[' + i + '].position'
            var avatarUrl = 'studentArray[' + i + '].avatarUrl'
            var id = 'studentArray[' + i + '].id'
            var imgShow='studentArray['+i+'].imgShow'
            var add = resData[i].position
          
            if(add.length>10){
              add=add.substring(0,9)+'..'
            }
            var type=resData[i].type
            console.log(type)
            var show=false
            if(type==3){
              show=true
            }
            that.setData({
              [imgShow]:show,
              [username]: resData[i].userNickName,
              [special]: resData[i].special,
              [courseName]: resData[i].courseName,
              [position]: add,
              [avatarUrl]: resData[i].userAvatarUrl,
              [id]: resData[i].id
            })
          }
        } catch (err) {
          console.log(err)
        }
      }
    })
    this.setData({
      subjectDisplay: !this.data.subjectDisplay,
      select: !this.data.select

    }) 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this
    wx.request({
      url: app.globalData.baseUrl+'demand/listcourse',
      method:'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        for(var i=0;i<res.data.data.data.length;i++){
          var id='subArray['+i+'].id'
          var value='subArray['+i+'].value'
          that.setData({
            [id]:res.data.data.data[i].id,
            [value]:res.data.data.data[i].name
          })
          console.log(that.data.subArray[i].id)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  getMoreStudentDemand:function(pageNo){
    var that = this

    wx.request({
      url: app.globalData.baseUrl + 'demand/listall',
      data: {
        pageNo:pageNo,
        pageSize:10,
        type: 1
      },
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        try {
          var resData = res.data.data.data
          for (var i = (pageNo-1)*10; i <((pageNo-1)*10)+resData.length; i++) {
            var username = 'studentArray[' + i + '].username'
            var special = 'studentArray[' + i + '].special'
            var courseName = 'studentArray[' + i + '].courseName'
            var position = 'studentArray[' + i + '].position'
            var avatarUrl = 'studentArray[' + i + '].avatarUrl'
            var id = 'studentArray[' + i + '].id'
            var imgShow = 'studentArray[' + i + '].imgShow'
            var add = resData[i%10].position
            console.log(add.length)
            if (add.length > 10) {
              add = add.substring(0, 9) + '..'
            } var type = resData[i].type
            console.log(type)
            var show = false
            if (type == 3) {
              show = true
            }
            that.setData({

              [imgShow]:show,
              [username]: resData[i%10].userNickName,
              [special]: resData[i%10].special,
              [courseName]: resData[i%10].courseName,
              [position]: add,
              [avatarUrl]: resData[i%10].userAvatarUrl,
              [id]: resData[i%10].id
            })
          }
        } catch (err) {
          console.log(err)
       
        }
      },
      fail: (err) => {
        console.log(err)
        wx.showToast({
          title: '获取数据失败！！！',
          icon: 'none'
        })
      }
    })
  },
  onShow: function () {
    this.getMoreStudentDemand(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     this.getMoreStudentDemand(this.data.pageNo)
     this.setData({
       pageNo:this.data.pageNo+1
     })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})